package com.song.service.Impl;

import com.song.controller.admin.RoleController;
import com.song.dao.RoleMapper;
import com.song.pojo.Role;
import com.song.service.RoleService;
import com.song.vo.RoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/10 - the current system date.
 * 21:50 - the current system time.
 */
@Service
@Transactional
public class RoleServiceImp implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    //查询角色列表
    public List<Role> findRoleList(RoleVo roleVo) {

        List<Role> roleList = roleMapper.findRoleList(roleVo);
        return roleList;
    }

    //添加角色
    public int addRole(Role role) {
        return roleMapper.addRole(role);
    }

    //修改角色
    public int updateRole(Role role) {
        return roleMapper.updateRole(role);
    }

    //删除角色
    public int deleteById(Integer id) {
        return roleMapper.deleteById(id);
    }

    //分配菜单
    public int saveRoleMenu(String ids, Integer roleId) {
        try {
             //删除原有的菜单关系
            roleMapper.deleteRoleMenu(roleId);//根据角色ID删除
            //将ids拆分成数组
            String [] idsStr = ids.split(",");
            for (int i = 0; i <idsStr.length ; i++) {
                //调用保存菜单角色关系的方法
                roleMapper.addRoleMenu(roleId,idsStr[i]);
            }
            return 1;//成功
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;//失败
    }

    //查询所有角色列表
    public List<Map<String, Object>> findRoleListByMap() {
        return roleMapper.findRoleListByMap();
    }

    //根据员工Id查询该员工拥有的角色列表
    public List<Integer> findEmployeeRoleByEmployeeId(Integer employeeId) {
        return roleMapper.findEmployeeRoleByEmployeeId(employeeId);
    }
}
